/**
 * 
 */
package br.com.games.daedal.ia;

import java.util.ArrayList;
import java.util.List;

import br.com.games.daedal.Box;
import br.com.games.daedal.DaedalTeam;
import br.com.games.daedal.Vertex;
import br.com.games.daedal.imp.DaedalBoard;
import br.com.games.daedal.imp.DaedalPlayer;
import br.com.games.hefesto.Game;
import br.com.games.hefesto.Move;

/**
 * Criar um jogador de Daedal
 * 
 * @since 2013
 * @author <a href="mailto:fernando.schindler@gmail.com">Fernando Schindler</a>
 */
public class Ares extends DaedalPlayer {
	/**
	 * 
	 * @param team
	 * @param level
	 */
	public Ares(DaedalTeam team, int level) {
		super(team, level/2);
		this.maxLevel=level;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.com.games.hefesto.Player#getValidMoves(br.com.games.hefesto.Game)
	 */
	@Override
	public List<Move<Vertex>> getValidMoves(Game<DaedalBoard, Vertex> game) {
		List<Move<Vertex>> result = new ArrayList<Move<Vertex>>();

		if (null == this.moves) {
			this.moves = new ArrayList<Move<Vertex>>();

			for (Vertex v : game.getState().getVertexes()) {
				this.moves.add(new Move<Vertex>(v));
			}
		}

		for (Move<Vertex> m : this.moves) {
			Vertex v = m.getMoveable();
			if (!v.isActive()) {			 
				result.add(m);	
			}
		}
		
		return result;
	}

	
	/*
	 * (non-Javadoc)
	 * @see br.com.games.hefesto.Player#evaluate(br.com.games.hefesto.Game)
	 */
	@Override
	public Short evaluate(Game<DaedalBoard, Vertex> game) {
		short value = 0;
		List<Move<Vertex>> list = null; 
			
		for (int i = 0; (level > 1) && i < game.getState().getSquares().size(); i++){
			Box box  = game.getState().getSquares().get(i);
			int n   = 0;
			Vertex f = null;
			
			for (Vertex v : box.getVertexes()){
				
				if (v.isActive()){
					n++;
				}
				else
					f = v;
			}
			
			if ((n==3) && (null != f)){
				if (null == list) list = new ArrayList<Move<Vertex>>();
				Move<Vertex> mm = getFromMoves(f);
				game.perform(mm);
				list.add(0, mm);
				i=-1;
			}
		}	 
		
		for (Box b : game.getState().getSquares()) {
		
			if (b.getOwner().equals(getTeam())) {
				value += b.getValue();
			} else
				if (!b.getOwner().equals(DaedalTeam.NONE))
				value -= b.getValue();
		}

		if (null != list){
			for (Move<Vertex> mm : list){
				game.undo(mm);
			}
			list=null;
		}
		
		return value;
	}
	
	/**
	 * 
	 * @param f
	 * @return
	 */
	private Move<Vertex> getFromMoves(Vertex f) {
		Move<Vertex> result = null;
		
		if (this.moves != null){
			for (Move<Vertex> m : this.moves){
				if (m.getMoveable().equals(f)){
					result = m;
					break;
				}
			}
		}
		else
			result = new Move<Vertex>(f);
		
		return result;
	}

	/*
	 * (non-Javadoc)
	 * @see br.com.games.hefesto.ai.Atena#getMove(br.com.games.hefesto.Game)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Move<Vertex> getMove(Game<? extends DaedalBoard, Vertex> game) {
		int moves = getValidMoves((Game<DaedalBoard, Vertex>) game).size() ;
		
		if (moves > 24)
			this.level = 1;
		else
		if (moves > 12)
			this.level = maxLevel/2;
		else
			this.level = maxLevel;
		
		return super.getMove(game);
	}
	
	/**
	 * Ajustar o nível do jogador
	 * @param level
	 */
	public void setLevel(int level){
		this.maxLevel = level;
	}
	
	/**
	 * Ajustar o nome do jogador
	 * @param newname
	 */
	public void setName(String newname){
		this.name=newname;
	}
	
	/*
	 * (non-Javadoc)
	 * @see br.com.games.hefesto.ai.Atena#getName()
	 */
	@Override
	public String getName() {
		if (name == null)
			return super.getName();
		else
			return name;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.com.games.hefesto.Player#giveUp()
	 */
	@Override
	public boolean giveUp() {
		return false;
	}
	
	/**
	 * Campos privados
	 */
	private List<Move<Vertex>> moves = null;
	private int maxLevel;
	private String name;
}
